package com.cloudkinto.controller.order.input;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.order.inbound.*;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dto.order.inbound.OrderInputProductTransitDto;
import com.cloudkinto.service.order.box.OrderBoxService;
import com.cloudkinto.service.order.input.OrderInputService;
import com.cloudkinto.vo.common.CommonBatchOperationVo;
import com.cloudkinto.vo.order.inbound.OrderInputDetailVo;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 到货通知单（订单入库） 前端控制器
 * </p>
 *
 * @author long
 * @since 2024-11-04
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Oms到货通知单模块")
@RequestMapping("/api/admin/oms/order/input")
public class OrderInputOmsController extends BaseController {
    private final OrderInputService orderInputService;

    private final OrderBoxService boxService;


    @UserSessionRequired
    @GetMapping("/getTransitProductList")
    @ApiOperation(value = "获取预售在途信息", notes = "获取预售在途信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "whId", value = "仓库"),
//            @ApiImplicitParam(name = "sku", value = "sku-模糊搜索"),
//            @ApiImplicitParam(name = "skuAccurate", value = "sku-精准搜索"),
//    })
    @ApiResponses(@ApiResponse(code = 200, message = "请求成功", response = OrderInputProductTransitDto.class))
    public SingleResult getTransitProductList(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", getCompanyId());
        String whId = request.getParameter("whId");
        map.put("whId", whId);
        String skuAccurate = request.getParameter("skuAccurate");
        map.put("skuAccurate", skuAccurate);
        String sku = request.getParameter("sku");
        map.put("sku", sku);
        return SingleResult.success("查询成功", orderInputService.getTransitProductList(map));
    }

    @UserSessionRequired
    @PostMapping("/getBoxTypeNumberList")
    @ApiOperation(value = "获取箱型列表")
    public PageResult getBoxTypeNumberListPage(@RequestBody InputBoxTypeQueryBo bo) {
        return boxService.getBoxTypeNumberListPage(bo);
    }

    @UserSessionRequired
    @ApiModelProperty(value = "判断箱类号能否修改产品")
    @PostMapping("/judgeUpdateBoxProductInfo")
    public SingleResult judgeUpdateBoxProductInfo(@RequestBody JudgeUpdateBoxProductInfoBo bo) {
        return SingleResult.success(orderInputService.judgeUpdateBoxProductInfo(bo));
    }

    @UserSessionRequired
    @PostMapping("/save")
    @ApiOperation(value = "入库新增修改内提交按钮", notes = "入库新增修改")
    public SingleResult save(@RequestBody @Validated OrderInputSaveBo bo) {
        orderInputService.save(bo, getCurrentUserId(), getUserName(), getTenantId(), getCompanyId());
        return SingleResult.success("正在处理中，可能需要一定时间，请稍后刷新查看");
    }

    @UserSessionRequired
    @PutMapping("/submit/{id}")
    @ApiOperation(value = "入库提交单个", notes = "入库提交单个")
    public SingleResult submit(@PathVariable("id") Long id) {
        orderInputService.submit(id, getCurrentUserId(), getUserName());
        return SingleResult.success("提交完成");
    }

    @UserSessionRequired
    @PutMapping("/submitBatch")
    @ApiOperation(value = "入库提交批量", notes = "入库提交批量")
    public SingleResult submitBatch(@RequestBody @Validated InboundSubmitBo bo) {
        orderInputService.submitBatch(bo, getCurrentUserId(), getUserName());
        return SingleResult.success("提交完成");
    }

    @UserSessionRequired
    @PutMapping("/cancel")
    @ApiOperation(value = "入库取消", notes = "入库取消")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = CommonBatchOperationVo.class))
    public SingleResult cancel(@RequestBody @Validated InboundCancelBo bo) {
        return SingleResult.success(orderInputService.cancel(bo, getCurrentUserId(), getUserName()));
    }


    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "入库查看详情", notes = "入库查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "请求成功", response = OrderInputDetailVo.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", orderInputService.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "入库查询初始化", notes = "入库查询初始化")
    public SingleResult pageInit() {
        return orderInputService.pageInit(getCompanyId(), getTenantId(), "oms", getCurrentUserId());
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "入库查询（分页）", notes = "入库查询（分页）")
    public PageResult listPage(@RequestBody @Validated OrderInputQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyIds(Collections.singletonList(getCompanyId()));
        return orderInputService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/listPageStatusStatistics")
    @ApiOperation(value = "分页查询状态数量统计", notes = "分页查询状态数量统计")
    public SingleResult listPageStatus(@RequestBody @Validated OrderInputQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setStatus(null);
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyIds(Collections.singletonList(getCompanyId()));
        return orderInputService.getListPageStatus(bo);
    }

    @UserSessionRequired
    @PostMapping("/excelExport")
    @ApiOperation(value = "导出到货通知单", notes = "导出到货通知单")
    public void excelExport(@RequestBody OrderInputQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("oms");
        bo.setCompanyIds(Collections.singletonList(getCompanyId()));
        orderInputService.excelExport(bo, getTenantId(), getCompanyId(), getWarehouseId(), response);
    }

    @UserSessionRequired
    @PostMapping("/printSkuCodeLabelPdf")
    @ApiOperation(value = "打印SKU标签", notes = "打印SKU标签")
    public void printSkuCodePdf(@RequestBody SkuCodePrintPdfBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        orderInputService.exportSkuLabel(bo, response);
    }

    @UserSessionRequired
    @PostMapping("/printPackageLabelPdf")
    @ApiOperation(value = "打印箱唛标签", notes = "打印箱唛标签")
    public void printPackageLabelPdf(@RequestBody PackageLabelPrintPdfBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        orderInputService.printPackageLabelPdf(bo, response);
    }

    @UserSessionRequired
    @PutMapping("/updateTrackingNumber")
    @ApiOperation(value = "更新跟踪号", notes = "更新跟踪号")
    public SingleResult updateTrackingNumber(@RequestBody @Validated InboundUpdateTrackingNumberBo bo) {
        return orderInputService.updateTrackingNumber(bo, getCurrentUserId(), getUserName());
    }

    @UserSessionRequired
    @PutMapping("/updateExpectedDate")
    @ApiOperation(value = "修改预计到货时间", notes = "修改预计到货时间")
    public SingleResult updateExpectedDate(@RequestParam("id") Long id, @RequestParam("expectedDate") String expectedDate) {
        orderInputService.updateExpectedDate(id, expectedDate, getCurrentUserId(), getUserName());
        return SingleResult.success("修改成功");
    }

    @UserSessionRequired
    @GetMapping("/getNormalInboundImportTemplate")
    @ApiOperation(value = "获取常规导入模板", notes = "获取常规导入模板")
    public void getNormalInboundImportTemplate(HttpServletResponse response) throws IOException {
        orderInputService.getNormalInboundImportTemplate(response, getTenantId(), getCurrentUserId(), getCompanyId(), "oms");
    }

    @UserSessionRequired
    @GetMapping("/getTransferInboundImportTemplate")
    @ApiOperation(value = "获取批发入库导入模板", notes = "获取批发入库导入模板")
    public void getTransferInboundImportTemplate(HttpServletResponse response) throws IOException {
        orderInputService.getTransferInboundImportTemplate(response, getTenantId(), getCurrentUserId(), getCompanyId(), "oms");
    }

    @UserSessionRequired
    @PostMapping("/importNormalInbound")
    @ApiOperation(value = "常规导入", notes = "常规导入")
    public SingleResult importNormalInbound(MultipartFile file) {
        return SingleResult.success(orderInputService.importNormalInbound(file, getTenantId(), getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PostMapping("/importTransferInbound")
    @ApiOperation(value = "批发入库导入", notes = "批发入库导入")
    public SingleResult importTransferInbound(MultipartFile file) {
        return SingleResult.success(orderInputService.importTransferInbound(file, getTenantId(), getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PostMapping("/postToJdyInputOrder")
    @ApiOperation(value = "调拨发货计划推送到筋斗云", notes = "调拨发货计划推送到筋斗云")
    public SingleResult<CommonBatchOperationVo> postToJdyInputOrder(@RequestBody @Validated InboundPostToJdyBo bo) {
        return orderInputService.postToJdyInputOrder(bo);
    }
}